package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.ResultInfo;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-01-05
 */
@RestController
@RequestMapping("/eduservice/teacher")
@Api(description = "讲师管理")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;



    //查询所有
    @GetMapping("/findAll")
    @ApiOperation("查询所有教师")
    public ResultInfo findAllTeacher(){
        List<EduTeacher> teacherList = eduTeacherService.list(null);
        return ResultInfo.ok().data("teacherList",teacherList);
    }

    //删除
    @DeleteMapping("/deleteTeacher/{id}")
    @ApiOperation("逻辑删除教师")
    public ResultInfo deleteTeacher(@ApiParam(name = "id",value = "教师ID",required = true) @PathVariable String id){
        boolean flag = eduTeacherService.removeById(id);
        if (flag){
            return ResultInfo.ok();
        }else {

            return ResultInfo.error();
        }
    }

    //分页查询
    @ApiOperation("分页查询")
    @GetMapping("/pageFindTeacher/{current}/{size}")
    public ResultInfo pageFindTeacher(
            @ApiParam (name = "current",value = "当前页码") @PathVariable long current,
            @ApiParam (name = "size",value = "每页显示条数") @PathVariable long size){
        Page<EduTeacher> pageInfo = new Page<>(current,size);
        eduTeacherService.page(pageInfo,null);
        long total = pageInfo.getTotal();
        List<EduTeacher> records = pageInfo.getRecords();
        return ResultInfo.ok().data("total",total).data("rows",records);
    }


    @ApiOperation("多条件分页查询")
    @PostMapping("/pageTeacherCondition/{current}/{size}")
    public ResultInfo pageTeacherCondition(
            @ApiParam (name = "current",value = "当前页码") @PathVariable long current,
            @ApiParam (name = "size",value = "每页显示条数") @PathVariable long size,
            @RequestBody(required = false) TeacherQuery teacherQuery){
        Page<EduTeacher> pageInfo = new Page<>(current,size);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }

        //排序
        wrapper.orderByDesc("gmt_create");

        eduTeacherService.page(pageInfo,wrapper);

        long total = pageInfo.getTotal();
        List<EduTeacher> records = pageInfo.getRecords();

        return ResultInfo.ok().data("total",total).data("rows",records);
    }

    @ApiOperation("添加讲师")
    @PostMapping("/addTeacher")
    public ResultInfo addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.save(eduTeacher);
        if (flag){
            return ResultInfo.ok();
        }else {

            return ResultInfo.error();
        }

    }

    @ApiOperation("根据ID查看讲师")
    @GetMapping("/getTeacher/{id}")
    public ResultInfo getTeacher(@PathVariable String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        return ResultInfo.ok().data("teacher",teacher);
    }

    @ApiOperation("修改讲师")
    @PostMapping("/editTeacher")
    public ResultInfo editTeacher(@RequestBody EduTeacher eduTeacher){

        boolean flag = eduTeacherService.updateById(eduTeacher);
        if (flag){
            return ResultInfo.ok();
        }else {

            return ResultInfo.error();
        }
    }














}

